<?php

Class Controller extends Core {

    function Controller() {
        $this->run_controller();
    }

    public static function path_info() {

        $uri = isset($_GET['q']) ? $_GET['q'] : request_uri();

        $pathInfo = preg_split('/\//', $uri, 4, PREG_SPLIT_NO_EMPTY);

        if (empty($pathInfo) || strpos($pathInfo[0], 'index.php') !== false)
            $pathInfo = array("default", "Default", "index");

        elseif (preg_match("/^\/Public\/.*(.html|.htm|.php|.shtml)/", $uri, $match))
            $pathInfo = array($match[0]);

        elseif (count($pathInfo) == 1) //特定页面                    
            $pathInfo = array($pathInfo[0]);

        elseif (count($pathInfo) == 2) //［默认模块］+模块类 + 逻辑处理函数
            $pathInfo = array("default", $pathInfo[0], $pathInfo[1]);

        elseif (count($pathInfo) == 3) { // (1)模块+模块类+逻辑处理函数 (2) ［默认模块］+模块类+逻辑处理函数+参数
            if (is_dir(APP_PATH . "modules/{$pathInfo[0]}") &&
                    file_exists(APP_PATH . "modules/{$pathInfo[0]}/{$pathInfo[1]}.Controller.php"))
                $pathInfo = array($pathInfo[0], $pathInfo[1], $pathInfo[2]);

            else
                $pathInfo=array("default", $pathInfo[0], $pathInfo[1], $pathInfo[2]);
        } elseif (count($pathInfo) == 4) { // 模块+模块类+逻辑处理函数+参数
            $pathInfo = array($pathInfo[0], $pathInfo[1], $pathInfo[2], $pathInfo[3]);
        }

        return array_slice($pathInfo, 0, 4);
    }

    private function run_controller() {

        $pathinfo = (array) self::path_info();

        //处理GET参数
        $this->_GET_($_GET);

        if (count($pathinfo) >= 3)
            $this->run_module($pathinfo[0], $pathinfo[1], $pathinfo[2]);

        else
            @include_once $pathinfo[0];
    }

    //返回链接参数对应的值

    function _GET_(&$arr=array()) {

        $pathInfo = self::path_info();

        if (count($pathInfo) == 4) {

            $query = base64_decode(str_replace(array(".html", ".htm", '.shtml'), array("==", '=', ''), end($pathInfo)));

            @parse_str($query, $outPut);

            $arr = array_merge($outPut, $arr);
        }
    }

    function run_module($module="default", $class="Default", $function="index") {

        if (!empty($module) && file_exists(APP_PATH . "modules/{$module}/{$module}Class.php")) {

            include_once(APP_PATH . "modules/{$module}/{$module}Class.php");

            $moduleClass = $module . "Class";

            $$module = new $moduleClass();
        }

        $class = ucwords($class);
        if (!empty($class) && file_exists(APP_PATH . "modules/{$module}/{$class}.Controller.php")) {

            include_once(APP_PATH . "modules/{$module}/{$class}.Controller.php");

            eval('$obj=new ' . $class . 'Controller();');

            //调用父类构造谐函数 (如果逻辑处理类定义了构造谐函数则)
            if (method_exists($obj, "C"))
                $obj->C();

            //加载全局类 $module."Class.php"
            if (is_object($obj))
                $obj->module = $$module;

            if (!empty($function) && method_exists($obj, $function))
                $obj->$function();
        }
    }

}